//
//  StatusCell.swift
//  TWeibo
//
//  Created by wtw on 15/8/3.
//  Copyright © 2015年 wtw. All rights reserved.
//

import UIKit

/// 控件之间的间距,关于魔法数字最好是要抽取出来，方便后期的修改
let statusCellContentMargin: CGFloat = 8.0

//微博cell的标示符枚举
enum StatusCellIndentifier: String {
    case NormolCell = "NormalCell"
    case ForwardCell = "ForwardCell"
    
    //根据微博数据，返回对应的标示符
    static func cellId(status: Status) -> String {
    return status.retweeted_status == nil ?
        StatusCellIndentifier.NormolCell.rawValue : StatusCellIndentifier.ForwardCell.rawValue
    }
    
}

class StatusCell: UITableViewCell {

    //微博数据模型
    var status: Status? {
        didSet {
            topView.status = status
            contentLabel.text = status?.text ?? ""
            
            //设置配图视图的数据
            pictureView.status = status
            
            // 设置配图视图的尺寸
            pictureHeightCons?.constant = pictureView.bounds.size.height
            pictureWidthCons?.constant = pictureView.bounds.size.width
            pictureTopCons?.constant = (pictureTopCons?.constant == 0) ? 0 : 8
        }
    }
    
    /// 图片宽度约束
    var pictureWidthCons: NSLayoutConstraint?
    /// 图片高度约束
    var pictureHeightCons: NSLayoutConstraint?
    //设置顶部约束
    var pictureTopCons: NSLayoutConstraint?

    //根据数据计算行高
    func rowHeight(status: Status) ->CGFloat {
        //设置属性
        self.status = status
        //强行更新布局,所有控件的frame都会发生变化
        //使用自动布局就不要直接修改frame,修改的工作一般是交给自动布局来完成
        layoutIfNeeded()
        //返回底部视图的最大高度
        return CGRectGetMaxY(bottomView.frame)
        
    }
    
    //MARK: - 界面设置
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置界面
     func setupUI() {
        //1.添加控件
        contentView.addSubview(topView)
        contentView.addSubview(contentLabel)
        contentView.addSubview(bottomView)
        contentView.addSubview(pictureView)
        //2.设置布局
        //1.顶部视图
        topView.ff_AlignInner(type: ff_AlignType.TopLeft, referView: contentView, size: CGSize(width: UIScreen.mainScreen().bounds.width, height: 53))
        //2.内容标签布局
        contentLabel.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: topView, size: nil,offset: CGPoint(x: statusCellContentMargin, y: statusCellContentMargin))
        // 宽度  主要和自动计算行高的属性联动使用，如果自己计算还需要设置这一属性
        contentView.addConstraint(NSLayoutConstraint(item: contentLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Width, multiplier: 1.0, constant: -2 * statusCellContentMargin))
//        // 3 配图视图
//        let cons = pictureView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: contentLabel, size: CGSize(width: 290, height: 290), offset: CGPoint(x: 0, y: statusCellContentMargin))
//        pictureWidthCons = pictureView.ff_Constraint(cons, attribute: NSLayoutAttribute.Width)
//        pictureHeightCons = pictureView.ff_Constraint(cons, attribute: NSLayoutAttribute.Height)
//        //顶部视图的图片的约束
//        pictureTopCons = pictureView.ff_Constraint(cons, attribute: NSLayoutAttribute.Top)
        
        //4.底部视图的布局
       bottomView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: pictureView, size: CGSize(width: UIScreen.mainScreen().bounds.width, height: 44), offset: CGPoint(x: -statusCellContentMargin, y:statusCellContentMargin ))
        //底部约束
//        contentView.addConstraint(NSLayoutConstraint(item: bottomView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 0))
    }
    
    //MARK: - 懒加载控件
    ///顶部视图
    private lazy var topView: StatusTopView = StatusTopView()
    /// 内容标签
    lazy var contentLabel: UILabel = {
        let label = UILabel(color: UIColor.darkGrayColor(), fontSize: 15)
        label.numberOfLines = 0
        //设置文本这折行，一旦达到一定的宽度就会折行（不然下面会出现一定的宽度）
        label.preferredMaxLayoutWidth = UIScreen.mainScreen().bounds.width - 2 * statusCellContentMargin
        return label
    }()
     /// 配图视图
    lazy var pictureView:StatusPictureView = StatusPictureView()
    
    /// 底部视图
    lazy var bottomView: StatusBottomView = StatusBottomView()
}



